<?php
namespace app\admin\controller;
use think\Validate;
use think\Db;
class Auth extends SessionCheck
{
    public function index()
    {
        $menu_list = $this->menu_list;
		$aid = input('get.aid');
               // dump($aid);exit;
		$phone = input('get.phone');
		$where = '';
        if(!empty($aid)){
            $where['aid'] = ['like','%'.$aid.'%'];
        }
        if(!empty($phone)){
            $where['phone'] = $phone;
        }
		$pages = pages('admin',$where,input('get.page'));
		$data = Db::table('admin')->where($where)->whereIn('aid',session('aid'))->limit($pages['limit'])->order('id desc')->select();
                //dump($data['0']['auth']);
            return view('',['data'  => $data,'pages' => $pages['pages'],'menu_list'=>$menu_list]);
    }
   // 
     public function edit()
    {
	$id = input('get.id')?input('get.id'):0;
        $menu_list = $this->menu_list;
		$menu = array();
		$province = Db::table('province')->where('status',1)->whereIn('id',session('aid'))->select();
		$city = Db::table('city')->where('status',1)->whereIn('id',session('aid'))->select();
                $area = Db::table('area')->where('status',1)->whereIn('id',session('aid'))->select();
		$topmenu = Db::table('menu')->where('gid',0)->select();
		if(!empty($topmenu)){
		      foreach($topmenu as $sub){
			   $sub['sub'] = Db::table('menu')->where('gid',$sub['id'])->select();
			   array_push($menu,$sub);
			}
		}
               // dump(session(''));
              //  dump($city);
             //   dump($area);
		if(request()->isPost()){
                 
		 $data = input('post.'); 
		 if($data['type'] == 'all'){
		 $validate = new Validate([
			 ['auth','require',lang('label')['auth'].lang('currency')['required']],
		          ]);
               
		        $data['auth'] = implode(',',$data['auth']);
                  
		}else{
                $data['all_aid'][] = $data['aid'];
                $validate = new Validate([
                    ['aid','require',lang('currency')['city'].lang('currency')['required']],
                    ['auth','require',lang('label')['auth'].lang('currency')['required']],
                ]);
                if($data['type'] == 'province'){
                    $city_str = Db::table('city')->where(['father_id'=>$data['aid']])->column('id');
                    $data['all_aid'] = array_merge($data['all_aid'],$city_str);
                    if(!empty($city_str)){
                        foreach($city_str as $cid){
                            $area_str = Db::table('area')->where(['father_id'=>$cid])->column('id');
                            $data['all_aid'] = array_merge($data['all_aid'],$area_str);
                        }
                    }
                 }elseif($data['type'] == 'city'){
                    $city_str = Db::table('area')->where(['father_id'=>$data['aid']])->column('id');
                    $data['all_aid'] = array_merge($data['all_aid'],$city_str);
                }
                 $data['auth'] = implode(',',$data['auth']);
                 $data['all_aid'] = implode(',',$data['all_aid']);
	        }
		if (!$validate->check($data)) {
			return error($validate->getError());
		     }
		if(Db::table('admin')->where(['phone' => $data['phone']])->where('id','<>',$id)->find()){
		       return error(lang('tip')['re_phone']);
		
		    }
			if($id){
				if(empty($data['password'])){
					unset($data['password']); 
				}else{
					$data['password'] = md5($data['password']);
				}
				Db::table('admin')->where(['id' => $id])->update($data);
				$data['id'] = $id;
				$data['password'] = '';
			}else{
				$data['time'] = time();
				$data['password'] = md5($data['password']);
				$data['id'] = Db::table('admin')->insertGetId($data);
			}
		}
               // dump($data);
		$data = Db::table('admin')->where(['id'=>['>',1],'id' => $id])->find();
              // dump($menu);
              // dump($menu_list[2]['child'][0]['auth']);
              // dump($menu[0]['sub']);
        return view('',['data'  => $data,'menu_all' => $menu,'province' => $province,'city'=>$city,'area'=>$area,'menu_list'=>$menu_list]);
    }
	
	public function del()
    {
		$id = input('get.id')?input('get.id'):0;
		$page = input('get.page');
		Db::table('admin')->delete($id);
		$this->redirect(url('auth/index').'?page='.$page);
		exit;
    }
}
